//
//  CYInvestRecordDetailViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/21.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYInvestRecordDetailViewController.h"

#import "CYEmptyCell.h"
#import "CYInvestRecordTopCell.h"
#import "CYInvestRecordProgressCell.h"

#import "CYWebViewController.h"



@interface CYInvestRecordDetailViewController ()

@property(nonatomic, strong)UIButton *contractButton;
@property(nonatomic, strong)UIView *contractLine;

@property(nonatomic, strong)CYInvestRecordTopCellObject *topCell;
@property(nonatomic, strong)CYInvestRecordProgressCellObject *progressCell;

@property(nonatomic, strong)CYInvestRecordDetailViewModel *model;
@property(nonatomic, assign)BOOL loading;

@end


@implementation CYInvestRecordDetailViewController

- (void)loadUI
{
    self.navigationItem.title = @"投资详情";
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    
    RAC(self.tableView, hidden) = [TCRACObserve(self, model) map:^id(id value) {
        return @(value == nil);
    }];
    
    self.contractButton = [[UIButton alloc] init];
    [self.contractButton addTarget:self action:@selector(onContractButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self.contractButton setBackgroundImage:[UIImage imageWithColor:HEXCOLOR(@"FFFFFF") size:CGSizeMake(1, 1)] forState:UIControlStateNormal];
    [self.contractButton setBackgroundImage:[UIImage imageWithColor:HEXCOLOR(@"ECECEC") size:CGSizeMake(1, 1)] forState:UIControlStateHighlighted];
    [self.contractButton setTitle:@"查看合同" forState:UIControlStateNormal];
    [self.contractButton setTitleColor:APP_DEFAULT_COLOR_BLUE forState:UIControlStateNormal];
    self.contractButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.contentView addSubview:self.contractButton];
    {
        self.contractLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contractButton addSubview:self.contractLine];
    }
    
    [self loadCell];
}

- (void)layoutUI
{
    self.tableView.width = self.contentView.width;
    self.tableView.height = self.contentView.height - 49;
    self.tableView.left = 0;
    self.tableView.top = 0;
    
    self.contractButton.width = self.contentView.width;
    self.contractButton.height = 49;
    self.contractButton.left = 0;
    self.contractButton.bottom = self.contentView.height;
    {
        self.contractLine.width = self.contractButton.width;
        self.contractLine.height = 1 / SCREEN_SCALE;
        self.contractLine.left = 0;
        self.contractLine.top = 0;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadDataFromServer];
}

- (void)onReceiveApplicationDidBecomeActiveNotification
{
    [self loadDataFromServer];
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    self.loading = YES;
    @weakify(self);
    if(self.model == nil)
    {
        [SVProgressHUD show];
    }
    [CYStandardDataAPI user_dueDetailWithInvest_detail_id:self.iid complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            if([response.content isKindOfClass:[NSDictionary class]])
            {
                CYInvestRecordDetailViewModel *model = [[CYInvestRecordDetailViewModel alloc] init];
                model.dueDay = SAFE_NUMBER(response.content[@"dueDay"]);
                model.title = SAFE_STRING(response.content[@"projectTitle"]);
                model.amount = SAFE_NUMBER(response.content[@"invSucc"]);
                model.rate = SAFE_NUMBER(response.content[@"userInterest"]);
                model.additionRate = SAFE_NUMBER(response.content[@"extraInterest"]);
                model.profits = SAFE_NUMBER(response.content[@"waitInterest"]);
                model.investDate = [[SAFE_NUMBER(response.content[@"addTime"]) cy_date] cy_stringWithFormat:@"yyyy-MM-dd"];
                model.profitsDay = SAFE_NUMBER(response.content[@"durationDay"]);
                model.contractUrl = SAFE_STRING(response.content[@"contractUrl"]);
                model.coupon = SAFE_NUMBER(response.content[@"couponAmount"]);
                
                NSDictionary *process = response.content[@"process"];
                if([process isKindOfClass:[NSDictionary class]])
                {
                    NSNumber *investTime = SAFE_NUMBER(process[@"investTime"]);
                    NSNumber *goldTime = SAFE_NUMBER(process[@"goldTime"]);
                    NSNumber *loanTime = SAFE_NUMBER(process[@"loanTime"]);
                    NSNumber *interestTime = SAFE_NUMBER(process[@"interestTime"]);
                    NSNumber *repaymentTime = SAFE_NUMBER(process[@"repaymentTime"]);
                    model.successDate = [investTime.cy_date cy_stringWithFormat:@"yyyy-MM-dd"];
                    model.accountDate = [goldTime.cy_date cy_stringWithFormat:@"yyyy-MM-dd"];
                    model.outDate = [loanTime.cy_date cy_stringWithFormat:@"yyyy-MM-dd"];
                    model.bearingDate = [interestTime.cy_date cy_stringWithFormat:@"yyyy-MM-dd"];
                    model.inDate = [repaymentTime.cy_date cy_stringWithFormat:@"yyyy-MM-dd"];
                }
                
                NSNumber *status = SAFE_NUMBER(response.content[@"status"]);
                if(status.integerValue == 0)
                {
                    model.progress = CYInvestProgressAccount;
                }
                else if(status.integerValue == 1 || status.integerValue == 3)
                {
                    model.progress = CYInvestProgressBearing;
                }
                else
                {
                    model.progress = CYInvestProgressIn;
                }
                
                self.model = model;
                self.topCell.model = self.model;
                self.progressCell.model = self.model;
                [self.tableView reloadData];
            }
            [SVProgressHUD dismiss];
        }
        else
        {
            if(self.model == nil)
            {
                [SVProgressHUD showErrorWithStatus:response.message];
            }
            else
            {
                [SVProgressHUD dismiss];
            }
        }
    }];
}

#pragma mark - Control Events
- (void)onContractButtonTouchUpInside
{
    if(self.model.contractUrl.length)
    {
        CYWebViewController *webViewController = [[CYWebViewController alloc] init];
        webViewController.url = self.model.contractUrl;
        [self.navigationController pushViewController:webViewController animated:YES];
    }
}

#pragma mark - Private Method
- (void)loadCell
{
    NSMutableArray *cells = [NSMutableArray array];
    {
        self.topCell = [CYInvestRecordTopCellObject objectWithCellClass:[CYInvestRecordTopCell class]];
        [cells addObject:self.topCell];
    }
    {
        CYEmptyCellObject *cell = [CYEmptyCellObject objectWithCellClass:[CYEmptyCell class]];
        cell.height = 10;
        [cells addObject:cell];
    }
    {
        self.progressCell = [CYInvestRecordProgressCellObject objectWithCellClass:[CYInvestRecordProgressCell class]];
        [cells addObject:self.progressCell];
    }
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:cells delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];
}

@end
